home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Graphics⁄Sound / Macintosh Tracker Folder / Trecker Server Folder / Main.c < prev    next >
C/C++ Source or Header  |  1993-05-30  |  6KB  |  282 lines

  1. /* Main.c */
  2.  
  3. /* this file is based on "Simple Trecker.c" by Frank Seide */
  4.  
  5.  
  6. #include <sound.h>
  7. #include <GestaltEqu.h>
  8. #include <Power.h>
  9. #include "PSyn.h"
  10. #include "STrI.h"
  11. #include "mac_event.h"
  12.  
  13. #define CREATORCODE ('∫Tsq')
  14. #define WAITNEXTEVENTDELAY (30)
  15.  
  16.  
  17. extern Boolean                                ReceivedOpenEventFlag;
  18. extern char                                        FakeKeyBuffer[MAXKEYS];
  19. extern int                                        KeyBufPtr;
  20.  
  21. extern Boolean                                QuitPending;
  22.  
  23. /* parameters controlling the synthesis, with handy default values. */
  24. extern short                                    AntiAliasing;
  25. extern short                                    StereoOn;
  26. extern unsigned short                    SamplingRate;
  27. extern short                                    NumRepeats;
  28. extern short                                    Speed;
  29. extern short                                    StereoMix;
  30. extern short                                    Loudness;
  31.  
  32. extern int                                        RecalibratePlayer; /* set when settings change */
  33.  
  34. extern FSSpec                                    GlobalFileSpec;
  35.  
  36.  
  37. long                                                    PowerManagerInfo = 0;
  38.  
  39. struct PChannel*                            pc = NULL;
  40. struct SoundTrack**                        strk = NULL;
  41.  
  42. Boolean                                                Pausing = false;
  43.  
  44.  
  45.  
  46. void                SetParameters(void);
  47.  
  48.  
  49. void                discard_buffer(void)
  50.     {
  51.         StopPChannel(pc, true);        /* stop playing with fadeout */
  52.         UnlinkSoundTrack(strk);        /* Unlink STrk from channel */
  53.     }
  54.  
  55.  
  56. void                HandleKeyBuffer(void)
  57.     {
  58.         WaitForEvent(15);
  59.      LoopPoint:
  60.         if (KeyBufPtr > 0)
  61.             {
  62.                 int                    Scan;
  63.                 int                    KeyTemp;
  64.  
  65.                 KeyTemp = (unsigned char)FakeKeyBuffer[0];
  66.                 for (Scan = 1; Scan < KeyBufPtr; Scan += 1)
  67.                     {
  68.                         FakeKeyBuffer[Scan - 1] = FakeKeyBuffer[Scan];
  69.                     }
  70.                 KeyBufPtr -= 1;
  71.                 switch (KeyTemp)
  72.                     {
  73.                         case '+':
  74.                 Loudness += 8;
  75.                 if (Loudness > 64)
  76.                   {
  77.                     Loudness = 64;
  78.                   }
  79.                             SetParameters();
  80.                             UpdateSoundTrack(strk);
  81.                             break;
  82.                         case '-':
  83.                 Loudness -= 8;
  84.                 if (Loudness < 0)
  85.                   {
  86.                     Loudness = 0;
  87.                   }
  88.                             SetParameters();
  89.                             UpdateSoundTrack(strk);
  90.                             break;
  91.                         case ' ':
  92.                             TogglePause();
  93.                             break;
  94.                         case '>':
  95.                             (**strk).musicRecord.fastForward = 3;
  96.                             UpdateSoundTrack(strk);
  97.                             break;
  98.                         case '|':
  99.                             (**strk).musicRecord.fastForward = 1;
  100.                             UpdateSoundTrack(strk);
  101.                             break;
  102.                         case '<':
  103.                             StopPChannel(pc,false);
  104.                             ResetPChannel(pc);
  105.                             StartPChannel(pc);
  106.                             break;
  107.                     }
  108.                 goto LoopPoint;
  109.             }
  110.     }
  111.  
  112.  
  113. void            SetParameters(void)
  114.     {
  115.         (**strk).musicRecord.speedFactor = (100 * Speed) / 50;
  116.         (*pc).antiAlias = AntiAliasing;
  117.         StereoPChannel(pc,(Boolean)StereoOn);
  118.  
  119.         if (NumRepeats == 0)
  120.             {
  121.                 (**strk).musicRecord.loopDetect = FALSE;
  122.             }
  123.          else
  124.             {
  125.                 (**strk).musicRecord.loopDetect = TRUE;
  126.             }
  127.  
  128.         if (Loudness >= 64)
  129.             {
  130.                 PChannelVolume(pc,-1,0x10000);
  131.             }
  132.          else
  133.             {
  134.                 PChannelVolume(pc,-1,(0x10000 * (long)Loudness) / 64);
  135.             }
  136.     }
  137.  
  138.  
  139. void            main(void)
  140.     {
  141.         short                    i;
  142.         short                    WorkingDirectoryRefNum;
  143.         OSErr                    Error;
  144.         long                    ProcTypeInfo;
  145.  
  146.         /* this may not be necessary, but I'll do it anyway to avoid problems */
  147.         InitGraf (&thePort);
  148.         InitFonts();
  149.         FlushEvents (everyEvent, 0);
  150.         InitWindows();
  151.         InitMenus();
  152.         TEInit();
  153.         InitDialogs (NULL);
  154.          InitCursor();
  155.  
  156.         if (!RegisterEventHandlers())
  157.             {
  158.                 return;
  159.             }
  160.         Error = Gestalt(gestaltPowerMgrAttr,&PowerManagerInfo);
  161.         if (Error != noErr)
  162.             {
  163.                 return;
  164.             }
  165.         Error = Gestalt(gestaltProcessorType,&ProcTypeInfo);
  166.         if ((Error != noErr) || (ProcTypeInfo == gestalt68000))
  167.             {
  168.                 FatalError(FatalError68020NeededID);
  169.                 return;
  170.             }
  171.  
  172.         if ((PowerManagerInfo & (1 << gestaltPMgrExists)) != 0)
  173.             {
  174.                 DisableIdle();
  175.             }
  176.  
  177. #ifdef AskForFile
  178.         {
  179.             Point                                Thing = {50,50};
  180.             StandardFileReply        R;
  181.  
  182.             /* this is only used for debugging when we don't have access to the interface */
  183.             /* program (since the interface program can't link to this program when it */
  184.             /* is being run under the debugger.) */
  185.             StandardGetFile(NULL,-1,NULL,&R);
  186.             GlobalFileSpec = R.sfFile;
  187.         }
  188. #else
  189.         /* waiting for open document command to come */
  190.         while (!ReceivedOpenEventFlag && !QuitPending)
  191.             {
  192.                 WaitForEvent(60);
  193.             }
  194.         if (QuitPending)
  195.             {
  196.                 goto ExitPoint;
  197.             }
  198. #endif
  199.  
  200.         if (OpenPChannel (CHANNELS, true, 445*2, &pc))
  201.             {
  202.                 FatalError(FatalErrorInternalError);
  203.                 goto ExitPoint;
  204.             }
  205.  
  206.         /* change to desired frequency */
  207.         pc->softFreq = (unsigned long)SamplingRate << 16;
  208.         pc->hardFreq = (unsigned long)SamplingRate << 16;
  209.  
  210.         /* set maximum volume for 4 voices. */
  211.         for (i = 0; i < 4; i += 1)
  212.             {
  213.                 PChannelVolume(pc,i,0x10000);
  214.             }
  215.  
  216.         /* now loading the song.  First, coerce the FSSpec into a working directory */
  217.         OpenWD(GlobalFileSpec.vRefNum,GlobalFileSpec.parID,
  218.             CREATORCODE,&WorkingDirectoryRefNum);
  219.         if (GetSoundTrack(WorkingDirectoryRefNum,GlobalFileSpec.name,0,&strk,FALSE))
  220.             {
  221.                 CloseWD(WorkingDirectoryRefNum);
  222.                 FatalError(FatalErrorNotASong);
  223.                 goto ExitPoint;
  224.             }
  225.         CloseWD(WorkingDirectoryRefNum);
  226.  
  227.         LinkSoundTrack (strk, pc);
  228.  
  229.         SetParameters();
  230.  
  231.         ResetPChannel (pc);
  232.         if (StartPChannel(pc))
  233.             {
  234.                 FatalError(FatalErrorInternalError);
  235.                 goto PreExitPoint;
  236.             }
  237.  
  238.         /* now we do event loop waiting for codes & finish... */
  239.         while (!QuitPending)
  240.             {
  241.                 if (RecalibratePlayer)
  242.                     {
  243.                         SetParameters();
  244.                         UpdateSoundTrack(strk);
  245.                     }
  246.                 HandleKeyBuffer(); /* calls WaitForEvent */
  247.                 /* is the soundtrack done? */
  248.                 if ((**strk).musicRecord.nextOne)
  249.                     {
  250.                         QuitPending = true;
  251.                     }
  252.             }
  253.  
  254.      PreExitPoint:
  255.         ClosePChannel(pc);
  256.  
  257.      ExitPoint:
  258.         if ((PowerManagerInfo & (1 << gestaltPMgrExists)) != 0)
  259.             {
  260.                 EnableIdle();
  261.             }
  262.     }
  263.  
  264.  
  265. void                TogglePause(void)
  266.     {
  267.         if (Pausing)
  268.             {
  269.                 Pausing = false;
  270.                 StartPChannel(pc);
  271.             }
  272.          else
  273.             {
  274.                 Pausing = true;
  275.                 StopPChannel(pc,false);
  276.                 while (Pausing && !QuitPending)
  277.                     {
  278.                         HandleKeyBuffer();
  279.                     }
  280.             }
  281.     }
  282.